/*
 * Copyright 2021 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.benchmarks.dmn.decisiontable;

import java.io.IOException;
import java.time.LocalDate;

import org.drools.benchmarks.common.AbstractBenchmark;
import org.drools.benchmarks.common.ProviderException;
import org.drools.benchmarks.dmn.util.DMNUtil;
import org.kie.api.KieServices;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.dmn.api.core.DMNContext;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNResult;
import org.kie.dmn.api.core.DMNRuntime;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.Warmup;

@Warmup(iterations = 40000)
@Measurement(iterations = 5000)
public class DMNDTChineseYearBenchmark extends AbstractBenchmark {

    private Resource dmnResource;
    private DMNRuntime dmnRuntime;
    private DMNModel dmnModel;
    private DMNContext dmnContext;

    @Setup
    public void setupResource() throws IOException {
        dmnResource = KieServices.get().getResources().newClassPathResource("dmn/ChineseLunarYears.dmn").setResourceType(ResourceType.DMN);
        dmnRuntime = DMNUtil.getDMNRuntimeWithResources(false, dmnResource);
        dmnModel = dmnRuntime.getModel("xls2dmn_2049ac43-5bc2-4c2c-bd80-51ccb05d8b92", "xls2dmn");
    }

    @Setup(Level.Iteration)
    @Override
    public void setup() throws ProviderException {
        dmnContext = dmnRuntime.newContext();
        dmnContext.set("Date", LocalDate.of(2021, 2, 12));
    }

    @Benchmark
    public DMNResult evaluateDecisionTable() {
        return dmnRuntime.evaluateAll(dmnModel, dmnContext);
    }

    public static void main(String[] args) throws Exception {
        DMNDTChineseYearBenchmark a = new DMNDTChineseYearBenchmark();
        a.setupResource();
        a.setup();
        System.out.println(a.evaluateDecisionTable());
    }
}
